package com.liusy.code.wizards;

import java.sql.Connection;
import java.sql.DriverManager;

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Text;

import com.liusy.code.Constant;
import com.liusy.code.DbHelp;

/**
 * 数据库连接信息
 * 
 * @author liusy
 *
 */
@SuppressWarnings( { "restriction" })
public class DbGenerationWizardPage extends AbsWizardPage 
{
	private Text driverTxt;
	private Text urlTxt;
	private Text userTxt;
	private Text pwdTxt;
	

	private ISelection selection;

	protected DbGenerationWizardPage(ISelection selection)
	{
		super("Code");
		setTitle("代码生成");
		setDescription("数据库连接信息");
		setMessage("注意：请正确填写如下信息！",IMessageProvider.INFORMATION);  
		this.selection = selection;
	}

	@Override
	public void createControl(Composite parent)
	{
		//创建一个新的Composite
		Composite container = new Composite(parent, SWT.NULL);
		//设置网格布局
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;//2列
		layout.verticalSpacing = 9;//垂直间距
		//---------------------------------------------------------------
		//---------------------------------------------------------------
		Label label1 = new Label(container, SWT.NULL);
		label1.setText("驱动:");
		driverTxt = new Text(container,  SWT.BORDER);
		driverTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		//---------------------------------------------------------------
		Label label2 = new Label(container, SWT.NULL);
		label2.setText("URL:");
		urlTxt = new Text(container, SWT.BORDER);
		urlTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		//---------------------------------------------------------------
		Label label3 = new Label(container, SWT.NULL);
		label3.setText("用户名:");
		userTxt = new Text(container, SWT.BORDER);
		userTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		//---------------------------------------------------------------
		Label label4 = new Label(container, SWT.NULL);
		label4.setText("密码:");
		pwdTxt = new Text(container, SWT.BORDER);
		pwdTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		//---------------------------------------------------------------
		
		Button button = new Button(container, SWT.NULL);
		button.setText("测试");
		button.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		button.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				canFlipToNextPage();
			}
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
			}
		});
		
		//设置焦点
		parent.setFocus();
		//初始化界面默认值
		initialize();
		dialogChanged();
		
		setControl(container);
		
	}

	/**
	 * 初始化界面默认值
	 */
	protected void initialize()
	{
		
		driverTxt.setText(Constant.driver);
		urlTxt.setText(Constant.url);
		userTxt.setText(Constant.user);
		pwdTxt.setText(Constant.pwd);
	}

	/**
	 * 验证
	 */
	private void dialogChanged()
	{
		
		if (driverTxt.getText().length() == 0
				||urlTxt.getText().length() == 0
				||userTxt.getText().length() == 0
				||pwdTxt.getText().length() == 0)
		{
			updateStatus("不能为空！！！");
			return;
		}
	}



	
	/**
	 * 下一步是否可用
	 */
	@Override
	public boolean canFlipToNextPage() {
		DbHelp db=null;
        try{
			 db= DbHelp.getDbHelp(Constant.driver, Constant.url, Constant.user, Constant.pwd);
        }catch(Exception e1){
         //  e1.printStackTrace();
           updateStatus("数据库连接不正确！");
           return false;
        }
        if (db != null) {
        	
    		Constant.driver=driverTxt.getText();
    		Constant.url=urlTxt.getText();
    		Constant.user=userTxt.getText();
    		Constant.pwd=pwdTxt.getText();

    		Constant.tableNameSourse = db.getAllDataTableArray();
			
			AbsWizardPage tableWp= (AbsWizardPage) this.getWizard().getNextPage(this);
			tableWp.initialize();
			
			return true;
        } else {
           updateStatus("数据库连接不正确！");
           return false;
        }
	}

	
	public static void main(String[] args) {
		  Connection conn=null;
	        try{
	           Class.forName(Constant.driver);
	           conn=DriverManager.getConnection(Constant.url,Constant.user,Constant.pwd);
	        }catch(Exception e1){
	           e1.printStackTrace();
	        }
	        
	        if (conn != null) {
	        	System.out.println("a");
	        	
	        }
	}
	
}
